﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Janksen.Mapping
{
    /// <summary>
    /// 生成SQL语句
    /// </summary>
    public class GenSqlCommandText
    {
        #region 插入数据语句
        /// <summary>
        /// 插入数据语句
        /// </summary>
        /// <param name="tableName">插入的表名</param>
        /// <param name="fieldList">插入的字段列表，多个字段逗号分隔。如：field1,field2,....</param>
        /// <returns></returns>
        public static string Insert(string tableName, string fieldList)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException("tableName", "应指定表名。");
            }
            if (string.IsNullOrEmpty(fieldList))
            {
                throw new ArgumentNullException("fieldList", "插入数据必须至少包含一个字段，多个字段用逗号分隔。如：Field1, Field2, Field3 ...");
            }

            string[] fieldArray = fieldList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            if (fieldArray == null || fieldArray.Length == 0)
            {
                throw new ArgumentNullException("fieldList", "插入数据必须至少包含一个字段，多个字段用逗号分隔。如：Field1, Field2, Field3 ...");
            }

            StringBuilder insertFieldPartBuilder = new StringBuilder();
            StringBuilder insertValuePartBuilder = new StringBuilder();

            foreach (string fieldName in fieldArray)
            {
                insertFieldPartBuilder.AppendFormat(" {0},", fieldName);
                insertValuePartBuilder.AppendFormat(" @{0},", fieldName.Trim(' ', '[', ']', '\r', '\n'));
            }

            StringBuilder cmdTextBuilder = new StringBuilder();
            cmdTextBuilder.Append("INSERT INTO ");
            if (!tableName.StartsWith("["))
            {
                cmdTextBuilder.Append("[");
            }
            cmdTextBuilder.Append(tableName);
            if (!tableName.EndsWith("]"))
            {
                cmdTextBuilder.Append("]");
            }
            cmdTextBuilder.Append(" (");

            cmdTextBuilder.Append(insertFieldPartBuilder.ToString().TrimEnd(' ', ','));
            cmdTextBuilder.Append(" ) VALUES (");
            cmdTextBuilder.Append(insertValuePartBuilder.ToString().TrimEnd(' ', ','));
            cmdTextBuilder.Append(" )");

            return cmdTextBuilder.ToString();
        }
        #endregion

        public static string Select()
        {
            return "";
        }

        public static string Update()
        {
            return "";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static string Delete(string tableName, string condition)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException("tableName", "应指定表名。");
            }

            StringBuilder cmdTextBuilder = new StringBuilder();
            cmdTextBuilder.Append("DELETE ");
            if (!tableName.StartsWith("["))
            {
                cmdTextBuilder.Append("[");
            }
            cmdTextBuilder.Append(tableName);
            if (!tableName.EndsWith("]"))
            {
                cmdTextBuilder.Append("]");
            }
            if (!string.IsNullOrEmpty(condition))
            {
                condition = condition.Trim(',', ' ');
                if (!condition.StartsWith("WHERE", StringComparison.OrdinalIgnoreCase))
                {
                    cmdTextBuilder.Append(" WHERE ");
                }
                cmdTextBuilder.Append(condition);
            }
            return cmdTextBuilder.ToString();
        }
    }
}
